package com.maomi.coder;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;

/**
 * 描述:
 * indexer
 *
 * @author luzhongfu
 * @create 2019-03-25 8:09 PM
 */
public class Indexer {

    private static String[] ids = {"1", "2"};
    private static String[] unIndex = {"Netherlands", "Italy"};
    private static String[] unStored = {"Amsterdam has lots of bridges", "Venice has lots of canals"};
    private static String[] text = {"Amsterdam", "Venice"};

    public IndexWriter getWriter() throws IOException {
        //1.创建文件目录
        Directory dir = FSDirectory.open(Paths.get("/Users/luzhongfu/Downloads/lucene/index"));
        //2.创建分词器
        Analyzer analyzer = new StandardAnalyzer();
        //3.创建写索引配置
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);

        //4.创建写索引
        IndexWriter indexWriter = new IndexWriter(dir,indexWriterConfig);

        return indexWriter;
    }


    public void write() throws IOException {
        Indexer indexer = new Indexer();
        IndexWriter writer = indexer.getWriter();
        FieldType fieldType = new FieldType();
        fieldType.setStored(true);//是否存储
        fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
        fieldType.setTokenized(true);//是否分词

        for (int i = 0 ; i < 2 ; i ++){
            Document doc = new Document();
            doc.add(new Field("id",ids[i],fieldType));
            doc.add(new Field("country", unIndex[i], fieldType));
            doc.add(new Field("contents", unStored[i], fieldType));
            doc.add(new Field("city", text[i], fieldType));
            writer.addDocument(doc);
        }
        writer.commit();
    }

    public void read() throws IOException, ParseException {
        Directory directory = FSDirectory.open(Paths.get("/Users/luzhongfu/Downloads/lucene/index"));
        DirectoryReader open = DirectoryReader.open(directory);

        IndexSearcher is = new IndexSearcher(open);
        Analyzer analyzer = new StandardAnalyzer();

        QueryParser query = new QueryParser("contents", analyzer);
        Query parse = query.parse("+has -bridges");
        TopDocs search = is.search(parse, 10);

        System.out.println("评分：" + search.getMaxScore());
        for (ScoreDoc doc:search.scoreDocs) {
            Document doc1 = is.doc(doc.doc);
            System.out.println(doc1.get("country"));
            System.out.println(doc1.get("contents"));
            System.out.println(doc1.get("city"));

            //查询评分
//            Explanation explain = is.explain(parse, doc.doc);
//            System.out.println(explain.toString());
        }
        open.close();

    }

    public static void main(String[] args) throws IOException, ParseException {
        Indexer indexer = new Indexer();
        indexer.read();
    }

}
